package com.zhsm.fuxi.mvp.ui.activitys

import android.content.Intent
import com.hankkin.reading.base.TakePhotoMvpActivity
import com.hankkin.reading.control.OrderControl
import com.hankkin.reading.control.UserControl
import com.hankkin.reading.event.EventMap
import com.zhsm.paipaiout.baselibrary.utils.MyStatusBarUtil
import com.zhsm.paipaiout.baselibrary.utils.RxBusTools
import com.zhsm.fuxi.R
import com.zhsm.fuxi.bean.UserBean
import com.zhsm.fuxi.control.AppManager
import com.zhsm.fuxi.mvp.contract.MyCenterContract
import com.zhsm.fuxi.mvp.presenter.MyCenterPresenter
import com.zhsm.fuxi.mvp.ui.dialog.MyCenterDialog
import com.zhsm.fuxi.utils.GlideUtils
import kotlinx.android.synthetic.main.activity_my_center.*
import org.devio.takephoto.model.TResult
import org.jetbrains.anko.sdk25.coroutines.onClick


/**
 * Created by GuZhC on 2018/10/19.
 * describe:
 */
class MyCenterActivity : TakePhotoMvpActivity<MyCenterPresenter>(), MyCenterContract.IView {

    val mMyCenterDialog: MyCenterDialog by lazy {
        MyCenterDialog(this)
    }


    override fun getLayoutId(): Int {
        return R.layout.activity_my_center
    }

    override fun registerPresenter() = MyCenterPresenter::class.java

    override fun isHasBus(): Boolean {
        return true
    }

    override fun initView() {
        MyStatusBarUtil.setColorForSwipeBackDrawerLayout(this, resources.getColor(R.color.white), 0)
        MyStatusBarUtil.setLightMode(this)
        setClick()
        UserControl.getCurrentUser()?.let {
            upInfo(it)
        }
    }

    override fun saveEmailSucces(it: String?) {
        getPresenter().updaateUserIn()
    }

 override fun savePhoneSucces(it: String?) {
     UserControl.logout()
     AppManager.finishAllActivity()
     startActivity(Intent(this, LoginActivity::class.java))
    }

    override fun saveNickSucces(it: String?) {
        getPresenter().updaateUserIn()
    }

    override fun changeHeadPicSucces(key: String) {
        getPresenter().updaateUserIn()
    }

    override fun updaateUserInfoSuccess(user: UserBean?) {
        if (mMyCenterDialog.isShowing) mMyCenterDialog.dismiss()
        user?.let {
            UserControl.setCurrentUser(it)
            upInfo(it)
        }
        hideLoding()
        RxBusTools.getDefault().post(EventMap.UpUserInfoEvent())
    }

    override fun loginOutSucess() {
        UserControl.logout()
        AppManager.finishAllActivity()
        startActivity(Intent(this, LoginActivity::class.java))
    }

    private fun upInfo(it: UserBean) {
        GlideUtils.loadImage(this, it.icon, myCentreHead, R.mipmap.deafult_head)
        mycenterTvName.text = it.nick
        mycenterTvEmail.text = it.email
        if (it.mobile?.length!! > 7)mycenterTvPhone.text = it.mobile.replaceRange(3, 7, "****")
    }

    fun setClick() {
        myCenterBack.onClick { finish() }
        myCentreHead.onClick {
            mMyCenterDialog.wht = MyCenterDialog.CHAGE_IMG
            setDiaogTakePhotoAndOutClick()
            mMyCenterDialog.show()
        }
        mycenterLlName.onClick {
            mMyCenterDialog.wht = MyCenterDialog.CHANGE_HEAD
            setDialogClick()
            mMyCenterDialog.show()
        }
        mycenterLlEmail.onClick {
            mMyCenterDialog.wht = MyCenterDialog.CHAGE_EMAIL
            setDialogClick()
            mMyCenterDialog.show()

        }
        mycenterPhone.onClick {
            mMyCenterDialog.wht = MyCenterDialog.CHAGE_PHONE
            setDialogClick()
            mMyCenterDialog.show()

        }
        myCenterLoginOut.onClick {
            mMyCenterDialog.wht = MyCenterDialog.LOGIN_OUT
            setDiaogTakePhotoAndOutClick()
            mMyCenterDialog.show()
        }
    }

    private fun setDiaogTakePhotoAndOutClick() {
        mMyCenterDialog.setonClickTakePhotoAndOut(object : MyCenterDialog.onClickTakePhotoAndOut {
            override fun loginOut() {
                getPresenter().loginOut()
            }

            override fun takePic() {
                takePicture()
            }

            override fun chosePic() {
                chosePicture(1)
            }
        })
    }

    override fun takeSuccess(result: TResult?) {
        super.takeSuccess(result)
        result?.let {
            getPresenter().changeHeadPic(it.image.compressPath)
        }
    }

    private fun setDialogClick() {
        mMyCenterDialog.setOnClickSave(object : MyCenterDialog.onClickSave {
            override fun saveInfo(s: String, wht: Int) {
                when (wht) {
                    MyCenterDialog.CHANGE_HEAD ->
                        getPresenter().changeNick(s)
                    MyCenterDialog.CHAGE_EMAIL ->
                        getPresenter().changeEmail(s)
                    MyCenterDialog.CHAGE_PHONE ->
                        getPresenter().changePhone(s)
                }
            }
        })
    }


}